package com.kksoft.readcm.objects;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import com.kksoft.readcm.activity.App;
import com.kksoft.readcm.b.d;
import com.kksoft.readcm.b.m;
import com.kksoft.readcm.c.a;

public class aa extends ThreadPoolExecutor
  implements a
{

private  int a;
  private  int b;
  private  int c;
  private boolean d;
  private ReentrantLock e;
  private Condition f;
  private Object g;
  private boolean h;
  private ArrayList<m> i;
  private a j;

  public aa(int k, int l)
  {
      super(k, l, 10L, TimeUnit.SECONDS, new PriorityBlockingQueue(11, new e()));
      a = 0;
      b = 0;
      c = 0;
      d = false;
      e = new ReentrantLock();
      f = e.newCondition();
      g = new Object();
      h = false;
      i = new ArrayList<m>();
  }

  public void a(m paramm)
  {
    if (!this.h)
    {
      if (paramm == null)
        throw new NullPointerException();
      if ((paramm instanceof d))
      {
      paramm.a(this);
      synchronized (this.g)
      {
        boolean bool = this.i.add(paramm);
        int k = this.b;
        aj localaj = new aj(paramm, k);
        execute(localaj);
      }
      }
    }
  }

  public final void a(a parama)
  {
    this.j = parama;
  }

  protected void afterExecute(Runnable runnable, Throwable throwable)
  {
	  super.afterExecute(runnable, throwable);
	  if(throwable != null || !(runnable instanceof Future))
	  {
		  if(throwable != null)
	            App.b((throwable));
	        Thread.currentThread().interrupt();
	  }
	  else
	  {
		  try {
			((Future)runnable).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
  }

  public final void b(m paramm)
  {
    synchronized (this.g)
    {
      boolean bool = this.i.remove(paramm);
      if (this.j != null)
        this.j.b(paramm);
      return;
    }
  }

  protected void beforeExecute(Thread paramThread, Runnable paramRunnable)
  {
    super.beforeExecute(paramThread, paramRunnable);
    this.e.lock();
    try
    {
      while (true)
      {
        boolean bool = this.d;
        if (!bool)
          return;
        this.f.await();
      }
    }
    catch (InterruptedException localInterruptedException)
    {
      while (true)
      {
        paramThread.interrupt();
        this.e.unlock();
      }
    }
    finally
    {
      this.e.unlock();
    }
  }

  public void c()
  {
    e();
    this.h=true;
    synchronized (this.g)
    {
      Iterator<m> localIterator = this.i.iterator();
      if (!localIterator.hasNext())
      {
        this.i.clear();
        a=0;
        b=0;
        c=0;
        return;
      }
      localIterator.next().n();
    }
  }

  public final void d()
  {
    if (!this.d)
      this.e.lock();
    try
    {
      this.d=true;
      return;
    }
    finally
    {
      this.e.unlock();
    }
    //throw localObject;
  }

  public final void e()
  {
    if (this.d)
      this.e.lock();
    try
    {
      this.d=false;
      this.f.signalAll();
      return;
    }
    finally
    {
      this.e.unlock();
    }
    //throw localObject;
  }

  public final boolean f()
  {
    synchronized (this.g)
    {
      if (this.i.size() > 0)
      {
        return true;
      }
      else
      return false;
    }
  }

  public final void g()
  {
    this.j = null;
    e();
    shutdown();
    try
    {
      boolean bool = awaitTermination(60L, TimeUnit.SECONDS);
      List localList = shutdownNow();
      return;
    }
    catch (InterruptedException localInterruptedException)
    {
      while (true)
        App.b(localInterruptedException);
    }
  }
}